package xyz.zyl2020.juc.thread;

import java.util.concurrent.TimeUnit;

/**
 * @author ZhuYinglong
 * @date 2020/10/8 0008
 */
public class ThreadDemo8 {

    final Object lock = new Object();

    public static void main(String[] args) throws InterruptedException {
        ThreadDemo8 threadDemo8 = new ThreadDemo8();

        Thread myThread1 = new Thread(() -> threadDemo8.waiting(), "myThread1");
        myThread1.start();

        Thread myThread2 = new Thread(() -> threadDemo8.waiting(), "myThread2");
        myThread2.start();

        // 当程对象调用wait(1)方法后，该线程进入TIMED_WAITING状态
        System.out.println(myThread1.getState());
        TimeUnit.SECONDS.sleep(2);
        System.out.println(myThread1.getState());
    }

    public void waiting() {
        synchronized (lock) {
            Thread thread = Thread.currentThread();
            if (thread.getName().equals("myThread1")) {
                try {
                    lock.wait(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
